<template>
  <div class="nodes">
    <div class="page-header">
      <h2>节点管理</h2>
      <el-button type="primary" @click="showCreateDialog">
        <el-icon><Plus /></el-icon>创建节点
      </el-button>
    </div>

    <el-card>
      <el-table
        v-loading="loading"
        :data="nodes"
        style="width: 100%"
      >
        <el-table-column prop="name" label="节点名称" />
        <el-table-column prop="cluster.name" label="所属集群" />
        <el-table-column prop="ip_address" label="IP地址" />
        <el-table-column prop="status" label="状态">
          <template #default="{ row }">
            <el-tag :type="getStatusType(row.status)">
              {{ getStatusText(row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="资源配置">
          <template #default="{ row }">
            <div>CPU: {{ row.cpu_cores }}核</div>
            <div>内存: {{ row.memory_gb }}GB</div>
            <div>磁盘: {{ row.disk_gb }}GB</div>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template #default="{ row }">
            <el-button-group>
              <el-button size="small" @click="editNode(row)">编辑</el-button>
              <el-button size="small" type="danger" @click="deleteNode(row)">删除</el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 创建/编辑节点对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="isEdit ? '编辑节点' : '创建节点'"
      width="500px"
    >
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="节点名称" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="所属集群" prop="cluster">
          <el-select v-model="form.cluster" placeholder="请选择集群">
            <el-option
              v-for="cluster in clusters"
              :key="cluster.id"
              :label="cluster.name"
              :value="cluster.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="IP地址" prop="ip_address">
          <el-input v-model="form.ip_address" />
        </el-form-item>
        <el-form-item label="CPU核心数" prop="cpu_cores">
          <el-input-number v-model="form.cpu_cores" :min="1" />
        </el-form-item>
        <el-form-item label="内存(GB)" prop="memory_gb">
          <el-input-number v-model="form.memory_gb" :min="1" />
        </el-form-item>
        <el-form-item label="磁盘(GB)" prop="disk_gb">
          <el-input-number v-model="form.disk_gb" :min="1" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitForm">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { Plus } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useCloudStore } from '../stores/cloud'

const store = useCloudStore()
const loading = ref(false)
const dialogVisible = ref(false)
const isEdit = ref(false)
const formRef = ref(null)

const form = ref({
  name: '',
  cluster: '',
  ip_address: '',
  cpu_cores: 1,
  memory_gb: 1,
  disk_gb: 1
})

const rules = {
  name: [
    { required: true, message: '请输入节点名称', trigger: 'blur' },
    { min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur' }
  ],
  cluster: [
    { required: true, message: '请选择所属集群', trigger: 'change' }
  ],
  ip_address: [
    { required: true, message: '请输入IP地址', trigger: 'blur' },
    { pattern: /^(\d{1,3}\.){3}\d{1,3}$/, message: '请输入正确的IP地址', trigger: 'blur' }
  ],
  cpu_cores: [
    { required: true, message: '请输入CPU核心数', trigger: 'blur' }
  ],
  memory_gb: [
    { required: true, message: '请输入内存大小', trigger: 'blur' }
  ],
  disk_gb: [
    { required: true, message: '请输入磁盘大小', trigger: 'blur' }
  ]
}

const nodes = ref([])
const clusters = ref([])

onMounted(async () => {
  await Promise.all([fetchNodes(), fetchClusters()])
})

async function fetchNodes() {
  loading.value = true
  try {
    await store.fetchNodes()
    nodes.value = store.nodes
  } catch (error) {
    ElMessage.error('获取节点列表失败')
  } finally {
    loading.value = false
  }
}

async function fetchClusters() {
  try {
    await store.fetchClusters()
    clusters.value = store.clusters
  } catch (error) {
    ElMessage.error('获取集群列表失败')
  }
}

function getStatusType(status) {
  const types = {
    running: 'success',
    stopped: 'info',
    error: 'danger'
  }
  return types[status] || 'info'
}

function getStatusText(status) {
  const texts = {
    running: '运行中',
    stopped: '已停止',
    error: '错误'
  }
  return texts[status] || status
}

function showCreateDialog() {
  isEdit.value = false
  form.value = {
    name: '',
    cluster: '',
    ip_address: '',
    cpu_cores: 1,
    memory_gb: 1,
    disk_gb: 1
  }
  dialogVisible.value = true
}

function editNode(node) {
  isEdit.value = true
  form.value = { ...node }
  dialogVisible.value = true
}

async function deleteNode(node) {
  try {
    await ElMessageBox.confirm(
      `确定要删除节点 "${node.name}" 吗？`,
      '警告',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
    )
    // TODO: 实现删除节点的逻辑
    ElMessage.success('删除成功')
    await fetchNodes()
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('删除失败')
    }
  }
}

async function submitForm() {
  if (!formRef.value) return
  
  try {
    await formRef.value.validate()
    // TODO: 实现创建/更新节点的逻辑
    ElMessage.success(isEdit.value ? '更新成功' : '创建成功')
    dialogVisible.value = false
    await fetchNodes()
  } catch (error) {
    ElMessage.error('表单验证失败')
  }
}
</script>

<style scoped>
.nodes {
  padding: 20px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.page-header h2 {
  margin: 0;
}
</style> 